package tt.dz.order.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import tt.dz.order.pojo.*;
import tt.dz.order.service.OperationManagementService;
import tt.dz.order.service.rpc.client.MsRpcClient;
import tt.dz.order.util.ExcelUtill;
import tt.dz.order.util.OrderAppConfig;
import tt.dz.order.util.OrderConstant.BizSystemCode;
import tt.dz.order.util.OrderConstant.LoginForOperResult;
import tt.dz.order.util.OrderConstant.OrderStatus;
import tt.dz.order.util.UUIDUtil;
import tt.dz.order.util.excel.ExcelTemplate;
import tt.dz.order.util.excel.ExportUtil;
import tt.dz.order.util.excel.Exporter;
import tt.dz.order.util.page.Pagination;
import tt.dz.order.vo.*;
import tt.dz.order.vo.LoginForOperVO.LoginForOperResponseVO;
import tt.dz.webframe.authen.entity.RoleAndPermConstants;

/**
 * @Title: 订单系统对操作员提供的http控制器接口
 * @author zhoutian
 * @date 2015-11-17
 */
@Controller
public class OperationController {
	private static Logger log = LoggerFactory.getLogger(OperationController.class);

	@Autowired
	private OperationManagementService operationManagementService;

	@Autowired
	private MsRpcClient msRpcClient;
	private static final String ORDER_OPER_ID_SESSIONKEY = "ORDER_OPER_ID";

	/**
	 * 订单系统运营管理操作员登录
	 */
	@RequestMapping(value = "/operationService/loginForOper", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ModelAndView loginForOper(@RequestParam(value = "mobileNo", required = true) String mobileNo,
			@RequestParam(value = "password", required = true) String password) {
		log.info("订单系统运营管理操作员登录controller层");
		try {
			LoginForOperVO loginForOperVO = new LoginForOperVO();
			loginForOperVO.setMobileNo(mobileNo);
			loginForOperVO.setPassword(password);
			loginForOperVO.setSysCode(BizSystemCode.OrderServer.value());
			String responseStr = msRpcClient.loginForOper(loginForOperVO);
			LoginForOperResponseVO loginForOperResponseVO = JSON.parseObject(responseStr, LoginForOperResponseVO.class);
			ModelAndView mav = new ModelAndView();
			if (loginForOperResponseVO.getIsSuccess() == LoginForOperResult.fail.value()) {
				mav.setViewName("/operationService/loginPage");
			} else {

			}
			return mav;
		} catch (Exception e) {
			log.error("订单系统运营管理操作员登录controller层发生异常", e);
			ModelAndView mav = new ModelAndView("/operationService/loginPage");
			return mav;
		}
	}

	/**
	 * 服务列表查询
	 */
	@RequestMapping(value = "/operationService/listOrderServiceInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listOrderServiceInfo() {
		log.info("服务列表查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<OrderServiceInfo> serviceInfoList = operationManagementService.listOrderServiceInfo();
			retVO.setBizData(serviceInfoList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("服务列表查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 查询所有的有效一级服务
	 */
	@RequestMapping(value = "/operationService/listLevel1OrderServiceInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listLevel1OrderServiceInfo() {
		log.info("查询所有的一级服务controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<OrderServiceInfo> serviceInfoList = operationManagementService.listLevel1OrderServiceInfo();
			retVO.setBizData(serviceInfoList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("查询所有的一级服务controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 查询所有的二级服务
	 */
	@RequestMapping(value = "/operationService/listLevel2OrderServiceInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listLevel2OrderServiceInfo() {
		log.info("查询所有的二级服务controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<OrderServiceInfo> serviceInfoList = operationManagementService.listLevel2OrderServiceInfo();
			retVO.setBizData(serviceInfoList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("查询所有的二级服务controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 服务基本信息新增/修改
	 * 
	 * @param orderServiceInfoStr
	 *            服务基本信息json字符串
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateOrderServiceInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateOrderServiceInfo(@RequestBody String orderServiceInfoStr, HttpSession session) {
		log.info("服务基本信息新增/修改controller层,orderServiceInfoStr:", orderServiceInfoStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			OrderServiceInfo orderServiceInfo = JSON.parseObject(orderServiceInfoStr, OrderServiceInfo.class);
			orderServiceInfo.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateOrderServiceInfo(orderServiceInfo);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("服务基本信息新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 根据服务号删除服务
	 */
	@RequestMapping(value = "/operationService/deleteOrderServiceInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteOrderServiceInfo(@RequestParam(value = "serviceNo", required = true) String serviceNo) {
		log.info("根据服务号删除服务controller层,serviceNo:", serviceNo);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteOrderServiceInfo(serviceNo);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("根据服务号删除服务controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 运营商提供服务的查询
	 */
	@RequestMapping(value = "/operationService/listProviderService", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listProviderService() {
		log.info("运营商提供服务的查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<ProviderService> providerServiceList = operationManagementService.listProviderService();
			retVO.setBizData(providerServiceList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("运营商提供服务的查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 运营商提供服务的新增/修改
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateProviderService", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateProviderService(
			@RequestParam(value = "providerServiceStr", required = true) String providerServiceStr,
			@RequestParam(value = "saveOrUpdate", required = true) String saveOrUpdate, HttpSession session) {
		log.info("运营商提供服务的新增/修改controller层,providerServiceStr:", providerServiceStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			ProviderService providerService = JSON.parseObject(providerServiceStr, ProviderService.class);
			providerService.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateProviderService(providerService, saveOrUpdate);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("运营商提供服务的新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 根据服务编号和运营商编号删除服务
	 */
	@RequestMapping(value = "/operationService/deleteProviderService", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteProviderService(@RequestParam(value = "serviceNo", required = true) String serviceNo,
			@RequestParam(value = "serviceProviderCode", required = true) String serviceProviderCode) {
		log.info("根据服务编号和运营商编号删除服务controller层,serviceNo,serviceProviderCode:", serviceNo + "," + serviceProviderCode);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteProviderService(serviceNo, serviceProviderCode);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("根据服务编号和运营商编号删除服务controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 活动规则的查询
	 */
	@RequestMapping(value = "/operationService/listActivityRule", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listActivityRule() {
		log.info("活动规则的查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<ActivityRule> activityRuleList = operationManagementService.listActivityRule();
			retVO.setBizData(activityRuleList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("活动规则的查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 活动规则的新增/修改
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateActivityRule", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateActivityRule(
			@RequestParam(value = "activityRuleStr", required = true) String activityRuleStr,
			@RequestParam(value = "saveOrUpdate", required = true) String saveOrUpdate, HttpSession session) {
		log.info("活动规则的新增/修改controller层,activityRuleStr:", activityRuleStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			ActivityRule activityRule = JSON.parseObject(activityRuleStr, ActivityRule.class);
			activityRule.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateActivityRule(activityRule, saveOrUpdate);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("活动规则的新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 根据服务编号和运营商编号删除活动规则
	 */
	@RequestMapping(value = "/operationService/deleteActivityRule", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteActivityRule(@RequestParam(value = "serviceNo", required = true) String serviceNo,
			@RequestParam(value = "serviceProviderCode", required = true) String serviceProviderCode) {
		log.info("根据服务编号和运营商编号删除活动规则controller层,serviceNo,serviceProviderCode:", serviceNo + "," + serviceProviderCode);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteActivityRule(serviceNo, serviceProviderCode);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("根据服务编号和运营商编号删除活动规则controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 单价计量单位查询
	 */
	@RequestMapping(value = "/operationService/listOrderUnit", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listOrderUnit() {
		log.info("单价计量单位查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<OrderUnit> orderUnitList = operationManagementService.listOrderUnit();
			retVO.setBizData(orderUnitList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("单价计量单位查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 单价计量单位的新增/修改
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateOrderUnit", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateOrderUnit(@RequestBody String orderUnitRuleStr, HttpSession session) {
		log.info("单价计量单位的新增/修改controller层,orderUnitRuleStr:", orderUnitRuleStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			OrderUnit ou = JSON.parseObject(orderUnitRuleStr, OrderUnit.class);
			ou.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateOrderUnit(ou);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("单价计量单位的新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 根据计量单位编号删除计量单位
	 */
	@RequestMapping(value = "/operationService/deleteOrderUnit", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteOrderUnit(@RequestParam(value = "unitNo", required = true) Integer unitNo) {
		log.info("根据计量单位编号删除计量单位controller层,unitNo:", unitNo);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteOrderUnit(unitNo);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("根据计量单位编号删除计量单位controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 资源信息查询
	 */
	@RequestMapping(value = "/operationService/listSysResource", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listSysResource() {
		log.info("资源信息查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<SysResource> sysResourceList = operationManagementService.listSysResource();
			retVO.setBizData(sysResourceList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("资源信息查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 有效资源信息查询
	 */
	@RequestMapping(value = "/operationService/listValidSysResource", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listValidSysResource() {
		log.info("资源信息查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<SysResource> sysResourceList = operationManagementService.listValidSysResource();
			retVO.setBizData(sysResourceList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("资源信息查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 资源信息新增/修改
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateSysResource", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateSysResource(@RequestBody String sysResourceStr, HttpSession session) {
		log.info("资源信息新增/修改controller层,sysResourceStr:", sysResourceStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			SysResource sr = JSON.parseObject(sysResourceStr, SysResource.class);
			sr.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateSysResource(sr);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("资源信息新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 资源删除
	 */
	@RequestMapping(value = "/operationService/deleteSysResource", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteSysResource(@RequestParam(value = "resourceId", required = true) String resourceId) {
		log.info("资源删除controller层,menuId:", resourceId);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteSysResource(resourceId);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("资源删除controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 菜单信息查询
	 */
	@RequestMapping(value = "/operationService/listSysMenu", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listSysMenu() {
		log.info("菜单信息查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<SysMenu> sysMenuList = operationManagementService.listSysMenu();
			retVO.setBizData(sysMenuList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("菜单信息查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 有效菜单信息查询
	 */
	@RequestMapping(value = "/operationService/listValidSysMenu", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listValidSysMenu() {
		log.info("菜单信息查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<SysMenu> sysMenuList = operationManagementService.listValidSysMenu();
			retVO.setBizData(sysMenuList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("菜单信息查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 菜单信息新增/修改
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateSysMenu", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateSysMenu(@RequestBody String sysMenuStr, HttpSession session) {
		log.info("菜单信息新增/修改controller层,sysMenuStr:", sysMenuStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			SysMenu sr = JSON.parseObject(sysMenuStr, SysMenu.class);
			sr.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateSysMenu(sr);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("菜单信息新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 菜单删除
	 */
	@RequestMapping(value = "/operationService/deleteSysMenu", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteSysMenu(@RequestParam(value = "menuId", required = true) String menuId) {
		log.info("菜单删除controller层,menuId:", menuId);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteSysMenu(menuId);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("菜单删除controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 角色与资源对应关系查询
	 */
	@RequestMapping(value = "/operationService/listSysRoleRes", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listSysRoleRes() {
		log.info("角色与资源对应关系查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<SysRoleRes> sysRoleResList = operationManagementService.listSysRoleRes();
			retVO.setBizData(sysRoleResList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("角色与资源对应关系查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 角色与资源对应关系新增/修改
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateSysRoleRes", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateSysRoleRes(@RequestParam(value = "sysRoleResStr", required = true) String sysRoleResStr,
			@RequestParam(value = "saveOrUpdate", required = true) String saveOrUpdate, HttpSession session) {
		log.info("角色与资源对应关系新增/修改controller层,sysRoleResStr:", sysRoleResStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			SysRoleRes sysRoleRes = JSON.parseObject(sysRoleResStr, SysRoleRes.class);
			sysRoleRes.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateSysRoleRes(sysRoleRes, saveOrUpdate);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("角色与资源对应关系新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 删除角色资源分配
	 */
	@RequestMapping(value = "/operationService/deleteSysRoleRes", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteSysRoleRes(@RequestParam(value = "roleId", required = true) String roleId,
			@RequestParam(value = "resourceId", required = true) String resourceId) {
		log.info("删除角色资源分配controller层,roleId,menuId:", roleId + "," + resourceId);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteSysRoleRes(roleId, resourceId);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("删除角色资源分配controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 角色与菜单对应关系查询
	 */
	@RequestMapping(value = "/operationService/listSysRoleMenu", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listSysRoleMenu() {
		log.info("角色与菜单对应关系查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<SysRoleMenu> sysRoleMenuList = operationManagementService.listSysRoleMenu();
			retVO.setBizData(sysRoleMenuList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("角色与菜单对应关系查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 角色与菜单对应关系新增/修改
	 */
	@RequestMapping(value = "/operationService/saveOrUpdateSysRoleMenu", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrUpdateSysRoleMenu(
			@RequestParam(value = "sysRoleMenuStr", required = true) String sysRoleMenuStr,
			@RequestParam(value = "saveOrUpdate", required = true) String saveOrUpdate, HttpSession session) {
		log.info("角色与菜单对应关系新增/修改controller层,sysRoleMenuStr:", sysRoleMenuStr);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			SysRoleMenu sysRoleMenu = JSON.parseObject(sysRoleMenuStr, SysRoleMenu.class);
			sysRoleMenu.setCreator((String) session.getAttribute(ORDER_OPER_ID_SESSIONKEY));
			operationManagementService.saveOrUpdateSysRoleMenu(sysRoleMenu, saveOrUpdate);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("角色与菜单对应关系新增/修改controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 删除角色菜单分配
	 */
	@RequestMapping(value = "/operationService/deleteSysRoleMenu", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteSysRoleMenu(@RequestParam(value = "roleId", required = true) String roleId,
			@RequestParam(value = "menuId", required = true) String menuId) {
		log.info("删除角色菜单分配controller层,roleId,menuId:", roleId + "," + menuId);
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			operationManagementService.deleteSysRoleMenu(roleId, menuId);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("删除角色菜单分配controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 订单系统角色查询
	 */
	@RequestMapping(value = "/operationService/listSysRole", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listSysRole() {
		log.info("订单系统角色查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<SysRole> sysRoleList = operationManagementService.listSysRole();
			retVO.setBizData(sysRoleList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("订单系统角色查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 运营商列表查询
	 */
	@RequestMapping(value = "/operationService/listMerchant", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listMerchant() {
		log.info("运营商列表查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<Merchant> merchantList = operationManagementService.listMerchant();
			retVO.setBizData(merchantList);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("运营商列表查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 运营商列表查询
	 */
	@RequestMapping(value = "/operationService/listMerchantByManagerId", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listMerchantByManagerId() {
		log.info("运营商列表查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			List<Merchant> merchantList = operationManagementService.listMerchant();
			Subject subject = SecurityUtils.getSubject();
			String id = (String) subject.getSession().getAttribute("dzSessionAttrUserId");
			PubOperInfo info = new PubOperInfo();
			info.setManagerId(id);
			info = info.getByManagerId();
			if (OrderAppConfig.GET().getMerchantCode().equals(info.getMerchantCode())) {
				retVO.setBizData(merchantList);
			} else {
				List<Merchant> merchans = new ArrayList<>();
				for (Merchant merchant : merchantList) {
					if (merchant.getMerchantCode() != null
							&& merchant.getMerchantCode().equals(info.getMerchantCode())) {
						merchans.add(merchant);
					}
				}
				retVO.setBizData(merchans);
			}

			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("运营商列表查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 订单报表查询
	 * 
	 * @author Anson
	 * @createDate 2016年4月26日
	 * @desc 根据传入进来的消费开始/结束时间,以及当前的服务编号(服务类型),当前卡号,去查询当前关联的订单信息
	 * @param request
	 * @return
	 */
	/**
	 * 运营商列表查询
	 */
	// @RequestMapping(value = "/operationService/queryOrderResport", produces =
	// "application/json;charset=UTF-8")
	// @ResponseBody
	public String queryOrderResport(HttpServletRequest request) {
		log.info("订单报表查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			QueryOrderResportVO queryOrderResportVO = JSON.parseObject(request.getParameter("queryOrderResportVO"),
					QueryOrderResportVO.class);
			// 调用服务层处理查询
			List<OrderQueryVO> orderQueryVOs = operationManagementService.queryOrder(queryOrderResportVO);
			retVO.setBizData(orderQueryVOs);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("订单报表查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 根据传入进来的消费开始/结束时间,以及当前的服务编号(服务类型),当前卡号,去查询当前关联的订单信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/operationService/export", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public byte[] export(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "curPageNo", required = true) int curPageNo,
			@RequestParam(value = "prePageNum", required = true) int prePageNum,HttpServletResponse response) {
		try {
		QueryOrderResportVO queryOrderResportVO = JSON.parseObject(param, QueryOrderResportVO.class);
		List<ExportOrderItem> list=	operationManagementService.export(queryOrderResportVO);


			byte[] b = null;
     LinkedHashMap<String, String> fieldMap = new LinkedHashMap();
			   fieldMap.put("订单号", "orderno");
			   fieldMap.put("所属电站", "servicestationno");
			   fieldMap.put("电站名称", "stationname");
			   fieldMap.put("电桩编号", "equipmentno");
			   fieldMap.put("用户卡号", "cardno");
			   fieldMap.put("开启方式", "cardtype");
			   fieldMap.put("服务类型", "name");
			   fieldMap.put("电量", "buynum");
			   fieldMap.put("金额", "totalprice");
			   fieldMap.put("开始时间", "starttime");
			   fieldMap.put("结束时间", "endtime");
			   b=ExcelUtill.getCSVByteArray("订单报表", fieldMap, list);
			/* 142:148 */       response.setHeader("Content-Disposition", "attachment;fileName=demo.csv");
			/* 143:149 */       response.setHeader("Content-Transfer-Encoding", "binary");
			/* 144:150 */       response.setContentType("application/vnd.ms-excel; charset=utf-8");
	     return  b;

//
//
//		ExcelTemplate<ExportOrderItem> excelTemplate = new ExcelTemplate<>();
//		excelTemplate.setList(list);
//		excelTemplate.setTitle("订单报表");
//		excelTemplate.setHeaders(ExportUtil.createHeaderInfo());
//		excelTemplate.setFields(ExportUtil.createFields());
//		Exporter<ExportOrderItem> exporter = new Exporter<>(excelTemplate);
//			exporter.export(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  null;
	}

	/**
	 * 根据传入进来的消费开始/结束时间,以及当前的服务编号(服务类型),当前卡号,去查询当前关联的订单信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/operationService/queryOrderResport", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryOrderReportPagination(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "curPageNo", required = true) int curPageNo,
			@RequestParam(value = "prePageNum", required = true) int prePageNum) {
		log.info("订单报表分页查询controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			Subject subject = SecurityUtils.getSubject();
			String id = (String) subject.getSession().getAttribute("dzSessionAttrUserId");
			PubOperInfo info = new PubOperInfo();
			info.setManagerId(id);
			info = info.getByManagerId();
			QueryOrderResportVO queryOrderResportVO = JSON.parseObject(param, QueryOrderResportVO.class);
			if (StringUtils.isEmpty(queryOrderResportVO.getMerchantCode())) {
				if (StringUtils.isEmpty(info.getMerchantCode())) {
					retVO.setRetCode("fail");
					retVO.setErrorMsg("请绑定运营商");
					return retVO.toJSONString();
				} else {
					if (!OrderAppConfig.GET().getMerchantCode().equals(info.getMerchantCode())) {
						queryOrderResportVO.setMerchantCode(info.getMerchantCode());
					}
				}
			}

			Pagination<OrderQueryVO> pagination = operationManagementService.queryPagition(queryOrderResportVO,
					curPageNo, prePageNum);
			retVO.setBizData(pagination);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("订单报表分页查询controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 根据传入进来的消费开始/结束时间,以及当前的服务编号(服务类型),当前卡号,去查询当前关联的订单信息(用于修改订单信息)
	 * 
	 * @param queryOrderResportVOStr
	 * @return
	 */
	@RequestMapping(value = "/operationService/queryOrderInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryOrderInfoPagination(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "curPageNo", required = true) int curPageNo,
			@RequestParam(value = "prePageNum", required = true) int prePageNum) {
		log.info("查询为支付并且未发送台账的订单controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			QueryOrderResportVO queryOrderResportVO = JSON.parseObject(param, QueryOrderResportVO.class);
			if (StringUtils.isEmpty(queryOrderResportVO.getOrderNo())) {
				retVO.setRetCode(RetVO.FailCode);
				retVO.setErrorMsg("订单号为空");
				return retVO.toJSONString();
			}
			queryOrderResportVO.setStatus(OrderStatus.unPay.value());
			Pagination<OrderQueryVO> pagination = operationManagementService.queryPagition(queryOrderResportVO,
					curPageNo, prePageNum, false);
			retVO.setBizData(pagination);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("查询为支付并且未发送台账的订单controller层出错", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

	/**
	 * 获取验证码
	 * 
	 * @return
	 */
	@RequestMapping(value = "/operationService/getCheckCode")
	@ResponseBody
	public String getCheckCode() {
		log.info("获取验证码controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			if (!operationManagementService.getCheckCode(retVO)) {
				retVO.setRetCode(RetVO.FailCode);
			}
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("获取验证码controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			return retVO.toJSONString();
		}
	}

	/**
	 * 修改订单金额
	 * 
	 * @return
	 */
	@RequestMapping(value = "/operationService/changeOrderAmount", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String changeOrderAmount(@RequestParam(value = "orderItemChangeVo", required = true) String itemChangeVo) {
		// public String changeOrderAmount(@RequestParam(value = "param",
		// required = true) String param,@RequestParam(value = "checkCode",
		// required = true) String checkCode
		// ,@RequestParam(value = "orderNo", required = true) String orderNo) {
		log.info("修改订单金额controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			OrderItemChangeVo OrderItem = JSON.parseObject(itemChangeVo, OrderItemChangeVo.class);
			if (StringUtils.isEmpty(OrderItem.validationParameters())) {
				retVO.setRetCode(RetVO.FailCode);
				retVO.setErrorMsg(OrderItem.validationParameters());
			}
			operationManagementService.updateOrderAmount(OrderItem, retVO);
			if (!StringUtils.isEmpty(retVO.getErrorMsg())) {
				retVO.setRetCode(RetVO.FailCode);
			}
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("修改订单金额controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			return retVO.toJSONString();
		}
	}

	/**
	 * 计算订单项金额
	 * 
	 * @return
	 */
	@RequestMapping(value = "/operationService/calculateOrderPrice", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String calculateOrderPrice(@RequestParam(value = "orderItemChangeVo", required = true) String itemChangeVo) {
		// public String changeOrderAmount(@RequestParam(value = "param",
		// required = true) String param,@RequestParam(value = "checkCode",
		// required = true) String checkCode
		// ,@RequestParam(value = "orderNo", required = true) String orderNo) {
		log.info("计算订单项金额controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			OrderItemChangeVo OrderItem = JSON.parseObject(itemChangeVo, OrderItemChangeVo.class);
			retVO.setBizData(operationManagementService.calculateOrderPrice(OrderItem));
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("计算订单项金额controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			return retVO.toJSONString();
		}
	}

	/**
	 * 查询订单修改历史
	 * 
	 * @return
	 */
	@RequestMapping(value = "/operationService/queryChangeOrderLog", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryChangeOrderLog(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "curPageNo", required = true) int curPageNo,
			@RequestParam(value = "prePageNum", required = true) int prePageNum) {
		log.info("查询订单修改历史controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			QueryOrderChangeLogResportVO vo = JSON.parseObject(param, QueryOrderChangeLogResportVO.class);
			Pagination<OrderChangeLogVo> bizData = operationManagementService.queryLogPagition(vo, curPageNo,
					prePageNum);
			retVO.setBizData(bizData);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("查询订单修改历史controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}
	/**
	 * 查询运营商按日汇总金额
	 *
	 * @return
	 */
	@RequestMapping(value = "/operationService/queryOrderAlreadyPaid", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryOrderAlreadyPaid(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "curPageNo", required = true) int curPageNo,
			@RequestParam(value = "prePageNum", required = true) int prePageNum) {
		log.info("查询运营商按日汇总金额controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			Subject subject = SecurityUtils.getSubject();
			String id = (String) subject.getSession().getAttribute("dzSessionAttrUserId");
			PubOperInfo info = new PubOperInfo();
			info.setManagerId(id);
			info = info.getByManagerId();
			OrderAlreadyPaidVo vo = JSON.parseObject(param, OrderAlreadyPaidVo.class);
			if (StringUtils.isEmpty(info.getMerchantCode())) {
				retVO.setErrorMsg("请绑定运营商");
				retVO.setRetCode("fail");
				return retVO.toJSONString();
			} else {
				if (!OrderAppConfig.GET().getMerchantCode().equals(info.getMerchantCode())) {
					vo.setMerchantCode(info.getMerchantCode());
				}
			}

			Pagination<OrderAlreadyPaid> bizData = operationManagementService.queryOrderAlreadyPaidPagition(vo, curPageNo,
					prePageNum);
			retVO.setBizData(bizData);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("查询运营商按日汇总金额controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}
	/**
	 * 查询运营商支付信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/operationService/queryOrderPay", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryOrderPay(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "curPageNo", required = true) int curPageNo,
			@RequestParam(value = "prePageNum", required = true) int prePageNum) {
		log.info("查询运营商支付信息controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			OrderPayVo vo = JSON.parseObject(param, OrderPayVo.class);
			Pagination<OrderAlreadyPaid> bizData = operationManagementService.queryOrderPayPagition(vo, curPageNo,
					prePageNum);
			retVO.setBizData(bizData);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("查询运营商支付信息controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}
	/**
	 * 查询运营商支付信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/operationService/queryOrderPayLog", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryOrderPayLog(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "curPageNo", required = true) int curPageNo,
			@RequestParam(value = "prePageNum", required = true) int prePageNum) {
		log.info("查询运营商支付信息controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			OrderPayLogVo vo = JSON.parseObject(param, OrderPayLogVo.class);

			Subject subject = SecurityUtils.getSubject();
			String id = (String) subject.getSession().getAttribute("dzSessionAttrUserId");
			PubOperInfo info = new PubOperInfo();
			info.setManagerId(id);
			info = info.getByManagerId();
			if (StringUtils.isEmpty(info.getMerchantCode())) {
				retVO.setErrorMsg("请绑定运营商");
				retVO.setRetCode("fail");
				return retVO.toJSONString();
			} else {
				if (!OrderAppConfig.GET().getMerchantCode().equals(info.getMerchantCode())) {
					vo.setMerchantCode(info.getMerchantCode());
				}
			}
			Pagination<OrderPayInfo> bizData = operationManagementService.queryOrderPayLogPagition(vo, curPageNo,
					prePageNum);
			retVO.setBizData(bizData);
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("查询运营商支付信息controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}
	/**
	 * 查询运营商支付信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/operationService/pay", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String pay(@RequestParam(value = "orderAlreadyPaid", required = true) String param,@RequestParam(value = "checkCode", required = true) String checkCode) {
		log.info("支付controller层");
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {


           List< OrderAlreadyPaid >vos = JSON.parseArray(param, OrderAlreadyPaid.class);

			Subject subject = SecurityUtils.getSubject();
			String dzSessionAttrUserId = (String) subject.getSession()
					.getAttribute(RoleAndPermConstants.SESSION_ATTR_USERID);
			MerchantVO mvo=new MerchantVO();
			for (OrderAlreadyPaid vo:vos) {
				if(vo.getPayPrice().doubleValue()<=0){
					throw  new  RuntimeException("金额不能必须大于0");
				}
				mvo=new MerchantVO();
				mvo=	mvo.queryMerchantByCode(vo.getMerchantCode());
				if(mvo==null){
					throw  new  RuntimeException("数据错误");
				}
				if(StringUtils.isEmpty(mvo.getAlipay())){
					throw  new  RuntimeException(mvo.getName()+"没有绑定支付宝");
				}
				OrderAlreadyPaid	voold=vo.selectinfoByMerchantCode(vo.getMerchantCode());
				if(voold.getPrice().subtract(voold.getAlreadyPaid()).doubleValue()<vo.getPayPrice().doubleValue()){
					throw  new  RuntimeException("金额大于差额");
				}
			}
			for (OrderAlreadyPaid vo:vos) {
				operationManagementService.pay(vo,checkCode );
			}
			return retVO.toJSONString();
		} catch (Exception e) {
			log.error("支付controller层", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setErrorMsg(e.getMessage());
			return retVO.toJSONString();
		}
	}

}
